using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ICeFramework.Win.UI
{
    public partial class MaskedTextBox : TextBox
    {
        private char[] aMskMask;
        private char[] aMask;
        private string tmpMask;        
        
        public string Mask
        {
            get { return tmpMask; }
            set 
            {
                tmpMask = value;
                SetMask();
                //this.Text = "";
            }
        }

        private void SetMask()
        {
            this.Text = tmpMask;
            this.Text = this.Text.Replace("#", "_");
            this.Text = this.Text.Replace("&", "_");
            aMask = this.Text.ToCharArray();
            aMskMask = this.Text.ToCharArray();

            for (int a = 0; a < tmpMask.Length; a++)
            {
                if ((tmpMask.Substring(a, 1) == "#") || (tmpMask.Substring(a, 1) == "&"))
                    aMask.SetValue('_', a);
                else aMask.SetValue(Convert.ToChar(tmpMask.Substring(a, 1)), a);
            }

            for (int a = 0; a < tmpMask.Length; a++)
                aMask.SetValue(Convert.ToChar(tmpMask.Substring(a, 1)), a);
        }

        public MaskedTextBox()
        {            
            InitializeComponent();
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            int sel = this.SelectionStart;
            bool b = true;
            //e.Handled = true;
            if (e.KeyCode == Keys.Delete)
            {                
                if ((tmpMask != "") && (sel < tmpMask.Length))
                {
                    string newText = this.Text;
                    
                    switch (tmpMask[sel].ToString())
                    {
                        case "#":
                            newText = newText.Substring(0, sel) + "_" + newText.Substring(sel + 1);
                            break;
                        default:
                            break;
                    }
                    this.Text = newText;
                    this.SelectionStart = sel;
                }
            }
            if (e.KeyCode == Keys.Back)
            {
                sel--;
                if ((tmpMask != "") && (sel < tmpMask.Length))
                {
                    string newText = this.Text;

                    switch (tmpMask[sel].ToString())
                    {
                        case "#":
                            newText = newText.Substring(0, sel) + "_" + newText.Substring(sel + 1);
                            break;
                        default:
                            break;
                    }
                    this.Text = newText;
                    this.SelectionStart = sel;
                }
            }
            if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right))
                b = false;
            e.Handled = b;
        }

        //protected override void OnKeyUp(KeyEventArgs e)
        //{
        //    e.Handled = true;
        //}

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            int sel = this.SelectionStart;
            e.Handled = true;
            if ((tmpMask != "") && (sel < tmpMask.Length))
            {
                string newText = this.Text;
                bool b = false;
                switch (tmpMask[sel].ToString())
                {
                    case "#":
                        if (Char.IsDigit(e.KeyChar))
                        {
                            newText = newText.Substring(0, sel) + e.KeyChar.ToString() + newText.Substring(sel + 1);
                            break;
                        }
                        else return;
                    case ".":
                    case "-":
                    case "\\":
                    case "/":
                    case ",":
                        sel++;
                        if (sel < this.Text.Length)
                        {
                            b = true;
                            SelectionStart = sel + 1;
                            OnKeyPress(e);
                        }
                        break;
                    default:
                        break;
                }
                this.Text = newText;
                if (!b)
                    this.SelectionStart = sel + 1;
                else this.SelectionStart = sel;
            }
        }

        //public void SetText(string txt)
        //{
        //    if (txt == string.Empty)
        //    {
        //        for (int a = 0; a < aMask.Length; a++)
        //            if ((tmpMask.Substring(a, 1) == "#") || (tmpMask.Substring(a, 1) == "&"))
        //                aMask.SetValue('_', a);
        //            else aMask.SetValue(tmpMask.Substring(a, 1), a);
        //    }
        //    else
        //    {
        //        for (int a = 0; a < aMask.Length; a++)
        //            if ((tmpMask.Substring(a, 1) == "#") || (tmpMask.Substring(a, 1) == "&"))
        //                aMask.SetValue(txt.Substring(a, 1), a);
        //            else aMask.SetValue(tmpMask.Substring(a, 1), a);
        //    }
        //    this.Text = "";
        //    for (int a = 0; a < aMask.Length; a++)
        //        this.Text += aMask.GetValue(a);
        //}
    }
}
